/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import Classes.User;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author alexander
 */
public class UserDAO implements UserDAOInterface {
    
    //final static String dbAddress = "jdbc:mysql://a0752765mysql1.mysql.univie.ac.at:3306/a0752765mysql1";
    //final static String dbUser = "a0752765";    
    //final static String dbUserPassword = "bmjzphcs";
    
        
     final static String dbAddress = "jdbc:mysql://127.0.0.1:3306/immoDB";
     final static String dbUser = "root";    
     final static String dbUserPassword = "geheim";
    

    

    @Override
    public Vector<User> readAllUsers() {
        
        String query = "SELECT * FROM USERS";
        Vector<User> allUsers = new Vector<User>();
        try {
            try {
                Class.forName("com.mysql.jdbc.Driver").newInstance();
            } catch (InstantiationException ex) {
                Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (ClassNotFoundException ex) {
            System.out.println("Treiberklasse nicht gefunden");            
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
            
        }
        
        Connection con;
        try {
            con = DriverManager.getConnection(dbAddress, dbUser, dbUserPassword);
     
        java.sql.Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(query);
    
        
        while(rs.next()){
            User newUser = new User();
            newUser.setUserID(rs.getInt("UserID"));
            newUser.setUserAlias(rs.getString("UserAlias"));;
            newUser.setUserFirstName(rs.getString("UserFirstName"));
            newUser.setUserLastName(rs.getString("UserLastName"));
            newUser.setUserEMailAddress(rs.getString("UserEMail"));
            newUser.setUserPassword(rs.getString("UserPassword"));
            allUsers.add(newUser);
        }

          } catch (SQLException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return allUsers;
    }

    @Override
    public User getUser(String userAlias) {
        User newUser = new User();
        String query = "SELECT * FROM USERS WHERE UserAlias='"+userAlias+"'";
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        Connection con;
        try {
            con = DriverManager.getConnection(dbAddress, dbUser, dbUserPassword);
            java.sql.Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(query);
        while(rs.next()){           
            newUser.setUserAlias(rs.getString("UserAlias"));
            newUser.setUserEMailAddress(rs.getString("UserEMail"));
            newUser.setUserFirstName(rs.getString("UserFirstName"));
            newUser.setUserLastName(rs.getString("UserLastName"));
            newUser.setUserID(rs.getInt("UserID"));
            newUser.setUserPassword(rs.getString("UserPassword"));
        }

           } catch (SQLException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return newUser;
    }

    @Override
    public boolean saveUser(User user) {
        
        int userID = user.getUserID();
        
        String userAlias = user.getUserAlias();
        String userFirstName= user.getUserFirstName();
        String userLastName= user.getUserLastName();
        String userPassword= user.getUserPassword();
        String userEMail = user.getUserEMailAddress();
        
        System.out.println(userFirstName);
        System.out.println(userLastName);
        System.out.println(userPassword);
        System.out.println(userEMail);
        int userRole=0;
        
        String query="INSERT INTO USERS(UserAlias,UserFirstName,UserLastName,UserPassword,"+ 
                      "UserEMail) VALUES('"+userAlias+"','"+userFirstName+"','"
                       +userLastName+"','"+userPassword+"','"+userEMail+"')";
        
        String query2="INSERT INTO USERS (UserAlias,UserFirstName,UserLastNAme,UserPassword,UserEMail) VALUES ('Hansi','Hinterseer','geheim','derbrandl@ovi.com')";
        
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        Connection con;
        try {
            con = DriverManager.getConnection(dbAddress, dbUser, dbUserPassword);
            java.sql.Statement stmt = con.createStatement();
            int rs = stmt.executeUpdate(query);
            
       } catch (SQLException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

        return true;
    }

    @Override
    public boolean checkPass(String userEMail, String userPass) {
        Boolean userFound=false;
        User newUser = new User();
        String query = "SELECT * FROM USERS WHERE UserEMail='"+userEMail+"'";
        
                
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        Connection con;
        try {
            con = DriverManager.getConnection(dbAddress, dbUser, dbUserPassword);
            java.sql.Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(query);
            
            
            while(rs.next()){
                
                newUser.setUserID(rs.getInt("UserID"));
                newUser.setUserAlias(rs.getString("UserAlias"));
                newUser.setUserFirstName(rs.getString("UserFirstName"));
                newUser.setUserLastName(rs.getString("UserLastName"));
                newUser.setUserPassword(rs.getString("UserPassword"));
                newUser.setUserEMailAddress(rs.getString("UserEMail"));
                userFound=true;
            }
            
            
            
            
       } catch (SQLException ex) {
            Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
        
        if (!userFound){
            return false;
        }
        if(newUser.getUserPassword().equalsIgnoreCase(userPass)){
            return true;
        }
        else {
            return false;
        }
        
    }

    @Override
    public boolean checkUserExists(String userAlias) {
        boolean userExists = false;
        
        Vector<User> allUsers =  this.readAllUsers();
        Iterator iter =  allUsers.iterator();
        
        while(iter.hasNext()){
            User newUser = new User();
            newUser = (User)iter.next();
            if(newUser.getUserAlias().equalsIgnoreCase(userAlias)){
                userExists = true;
            }
            else {}
            
        }

        return userExists;
    }


}
